package com.JSON;

/**
 * 输出格式化的JSON数据
 */
public class FormatJSON {
    public String initJSON; // 初始 数据
    public String formJSON; // 格式化 数据
    public static final int TYPE_OTHER_CHAR = 0;
    public static final int TYPE_LINE_HEAD = 1;
    public static final int TYPE_LEFT_BRACKET = 2;
    public static final int TYPE_RIGHT_BRACKET_END = 3;

    public FormatJSON(String JSON) {
        if (JSON == null) {
            System.out.println("\nNULL data of com.JSON ------------------------------------>");
            return;
        }
        JSON = deleteSpace(JSON);
        System.out.println("\n格式化之前的 < com.JSON > 数据：\n" + JSON);
        this.initJSON = JSON;
        formatJSON(JSON);
    }

    /**
     * @param JSON data
     * @return 删除所有 空白类 的字符
     */
    private String deleteSpace(String JSON) {
        JSON = JSON.replace(" ", "");
        JSON = JSON.replace("\t", "");
        JSON = JSON.replace("\n", "");
        return JSON;
    }

    private void formatJSON(String JSON) {
        if (JSON.length() == 0) return;
        JSON = JSON.replace("{", "{\n");
        JSON = JSON.replace("}", "\n}");
        JSON = JSON.replace("[", "[\n");
        JSON = JSON.replace("]", "\n]");
        JSON = JSON.replace(",", ",\n");
        JSON = JSON.replace("]\n,", "],");
        JSON = JSON.replace("}\n,", "},");
        JSON = JSON.replace("\n\n", "\n");
        JSON = JSON.replace("{\n}", "{}");
        JSON = JSON.replace("[\n]", "[]");
        // com.JSON = addTable(com.JSON);
        JSON = "\n格式化之后的 < com.JSON > 数据：\n" + JSON;
        this.formJSON = JSON;
    }

    /**
     * @param JSON data
     * @return 添加缩进
     */
    private String addTable(String JSON) {
        int len = JSON.length();
        int tabCount = 0;
        for (int i = 0; i < len; i++) {
            int charType = getCharType(i, JSON);
            switch (charType) {
                case TYPE_LEFT_BRACKET:
                    if (tabCount < 1) tabCount = 1;
                    else {
                        JSON = addTable(JSON, i, tabCount);
                        i += tabCount;
                        tabCount++;
                        len = JSON.length();
                        System.out.println("\n\n\n --------------------------------------------------------------------\n" + JSON);
                    }
                    break;
                case TYPE_RIGHT_BRACKET_END:
                    if (tabCount > 0) {
                        tabCount--;
                        JSON = addTable(JSON, i, tabCount);
                        i += tabCount;
                        len = JSON.length();
                        break;
                    }
                case TYPE_LINE_HEAD:
                    if (tabCount > 0) {
                        JSON = addTable(JSON, i, tabCount);
                        i += tabCount;
                        len = JSON.length();
                    }
                    break;
                case TYPE_OTHER_CHAR:
                default:
                    break;
            }
        }
        return JSON;
    }

    private String addTable(String JSON, int i, int tabCount) {
        if (tabCount > 0) {
            String tabsLeft = getTabs(tabCount);
            JSON = JSON.substring(0, i) + tabsLeft + JSON.substring(i, JSON.length() - 1);
        }
        return JSON;
    }

    private int getCharType(int i, String JSON) {
        if (i < 0 || i >= JSON.length()) System.out.println();
        char ch = JSON.charAt(i);
        if ((ch == '{' || ch == '[') && ((i > 0 && JSON.charAt(i - 1) == '\n') || i == 0)) return TYPE_LEFT_BRACKET;
        if (i > 0 && (ch == '}' || ch == ']')) return TYPE_RIGHT_BRACKET_END;
        if (i > 0 && ch == '\"' && JSON.charAt(i - 1) == '\n') return TYPE_LINE_HEAD;
        return TYPE_OTHER_CHAR;
    }

    private String getTabs(int tabCount) {
        StringBuilder tabs = new StringBuilder();
        while (tabCount != 0) {
            tabs.append("\t");
            tabCount--;
        }
        return tabs.toString();
    }
}
